import redis
from django.conf import settings

class RedisClient:
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._redis = redis.Redis(
                host=settings.REDIS_HOST,
                port=settings.REDIS_PORT,
                db=settings.REDIS_DB,
                password=settings.REDIS_PASSWORD,
                decode_responses=True
            )
        return cls._instance
    
    def get(self, key):
        return self._redis.get(key)
    
    def set(self, key, value, ex=None):
        return self._redis.set(key, value, ex=ex)
    
    def delete(self, key):
        return self._redis.delete(key)
    
    def exists(self, key):
        return self._redis.exists(key)
    
    def expire(self, key, time):
        return self._redis.expire(key, time)

# 创建全局Redis客户端实例
redis_client = RedisClient()